package com.njit.farm.service.impl;

import com.njit.farm.dao.FReapedPlantMapper;
import com.njit.farm.dataobject.FReapedPlant;
import com.njit.farm.model.ReapedPlant;
import com.njit.farm.model.ReapedPlantState;
import com.njit.farm.model.Result;
import com.njit.farm.service.ReapedPlantService;
import com.njit.farm.util.IDUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ReapedPlantServiceImpl implements ReapedPlantService {

    @Resource
    FReapedPlantMapper reapedPlantMapper;

    @Override
    public Result add( ReapedPlant reapedPlant) {

        Result result = new Result();

        FReapedPlant fReapedPlant = new FReapedPlant();
        fReapedPlant.setRpId(IDUtils.uuid());
        fReapedPlant.setRpNum(reapedPlant.getNum());
        fReapedPlant.setRpTag(reapedPlant.getTag());
        fReapedPlant.setRpState(ReapedPlantState.REAPED.getState());
        fReapedPlant.setuId(reapedPlant.getuId());

        reapedPlantMapper.insert(fReapedPlant);

        result.setSucc(true);

        return result;
    }

    @Override
    public Result queryByUserId(String uid) {

        Result result = new Result();

        List<FReapedPlant> fReapedPlants = reapedPlantMapper.selectByUserId(uid);

        List<ReapedPlant> reapedPlantList = new ArrayList<>();
        for (FReapedPlant fReapedPlant : fReapedPlants) {
            reapedPlantList.add(fReapedPlant.toModel());
        }

        result.setSucc(true);
        result.setData(reapedPlantList);

        return result;
    }

    @Override
    public Result update(ReapedPlant reapedPlant) {

        Result result = new Result();

        FReapedPlant fReapedPlant = new FReapedPlant();
        fReapedPlant.setRpId(reapedPlant.getId());
        fReapedPlant.setRpNum(reapedPlant.getNum());
        fReapedPlant.setRpState(reapedPlant.getState());
        fReapedPlant.setRpTag(reapedPlant.getTag());
        fReapedPlant.setuId(reapedPlant.getuId());

        reapedPlantMapper.updateByPrimaryKey(fReapedPlant);

        result.setSucc(true);
        return result;
    }
}
